package com.novellotus.iov.carvice.retrofit;

import android.content.Context;
import android.util.Log;

import com.novellotus.iov.carvice.utils.MyUtils;

import java.util.List;

import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

/**
 * Created by Rich on 2017/8/18.
 */

public abstract class BaseArrayObserver<T> implements Observer<BaseArrayEntity<T>> {
    private static final String TAG = "Rich";
    private Context context;

    protected BaseArrayObserver(Context context) {
        this.context = context.getApplicationContext();
    }

    @Override
    public void onSubscribe(Disposable d) {

    }

    @Override
    public void onNext(BaseArrayEntity<T> value) {
        if (value.isSuccess()) {
            //  List<T> t = value.getData();
            onSuccess(value.getData());
        } else {
            onFail(value.getCode(), value.getMsg());
        }
    }

    @Override
    public void onError(Throwable e) {
        MyUtils.logInformation(e.toString());
        if (MyUtils.isTimeOut(e.toString())) {
            onError("链接超时");
        } else if (MyUtils.isIllegalState(e.toString())) {
            onError("数据异常");
        } else {
            onError("网络异常");
        }
    }

    @Override
    public void onComplete() {
        Log.e(TAG, "onComplete");
    }

    public abstract void onSuccess(List<T> t);

    public abstract void onFail(int code, String msg);

    public abstract void onError(String msg);

}
